package com.yict.csms.system.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.yict.common.dao.impl.CommonDao;
import com.yict.common.util.DateUtil;
import com.yict.csms.system.entity.Holiday;

/**
 * 
 * 
 * @author Patrick.Deng
 * 
 */
@Repository
public class HolidayDaoImpl extends CommonDao<Holiday, Long> {
	/**
	 * 查找当前所有的年度
	 * @return
	 */
	public List<Date> findAnnual() {
		List<Date> list = new ArrayList<Date>();
		String queryString = "select c.annual from Holiday as c where 1=1 and c.annual is not null order by c.createDate desc";
		try {
			Query query = this.getSession().createQuery(queryString);
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	/**
	 * 根据年度和日期查找记录
	 * @param annual
	 * @param date
	 * @param id 
	 * @return true-存在
	 */
	public boolean check(Date annual, Date date, boolean begin, Long id) {
		List<Holiday> list = new ArrayList<Holiday>();
		StringBuilder queryString = new StringBuilder("from Holiday as c where 1=1 ");
		String bDate = "";
		String endDate = "";
		if(annual != null) {
			queryString.append(" and c.annual >= :annualDate and c.annual <= :annualEndDate");
			bDate = DateUtil.DateToString(annual, 4) + "-1-1";
			endDate =  DateUtil.DateToString(annual, 4) + "-12-31";
		}
		
		if(begin) {
			queryString.append(" and (c.beginDate =:beginDate or c.endDate = :beginDate) ");
		} else {
			queryString.append(" and (c.endDate =:beginDate or c.beginDate = :beginDate)");
		}
		if(id != null) {
			queryString.append(" and c.holidayId not in (:id)");
		}
		try {
			Query query = this.getSession().createQuery(queryString.toString());
			if(annual != null) {
				query.setParameter("annualDate", DateUtil.StringToDate(bDate, 1));
				query.setParameter("annualEndDate", DateUtil.StringToDate(endDate, 1));
			}
			query.setParameter("beginDate", date);
			if(id != null) {
				query.setParameter("id", id);
			}
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(list != null && list.size() > 0) {
			return true;
		}
		return false;
	}
	/**
	 * 根据假日名称查找记录
	 * @param holidayName
	 * @param id 
	 * @return true-存在
	 */
	public boolean check(String holidayName, Long id) {
		List<Holiday> list = new ArrayList<Holiday>();
		StringBuilder queryString = new StringBuilder("from Holiday as c where 1=1 and c.holidayName =:holidayName ");
		if(id != null) {
			queryString.append(" and c.holidayId not in (:id)");
		}
		try {
			Query query = this.getSession().createQuery(queryString.toString());
			query.setParameter("holidayName", holidayName);
			if(id != null) {
				query.setParameter("id", id);
			}
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(list != null && list.size() > 0) {
			return true;
		}
		return false;
	}
	
	public Holiday findById(Long id) {
		return (Holiday)this.getTemplate().get(Holiday.class, id);
	}

}
